//
//  IPInputPlanSelectTimeView.swift
//  IPlan
//
//  Created by robot on 3/29/16.
//  Copyright © 2016 codeFighter.com. All rights reserved.
//

import UIKit

class IPInputPlanSelectTimeView: UIView {
    
    var inputPlanCloure: ((_ string:String) ->(Void))?
    fileprivate var inputPlanCellIdentifier = "IPInputPlanSelectTimeView"
    fileprivate var inputDatePicker:UIDatePicker?
    func setup() {
        
        // add topContentView
        let datePickerH:CGFloat = 250.0
        let topContentH:CGFloat = 49.0
        let topContentY:CGFloat = SCREEN_H - datePickerH - topContentH
        let topContentRect:CGRect = CGRect(x: 0, y: topContentY, width: SCREEN_W, height: topContentH)
        let topContentView = UIView(frame:topContentRect)
        topContentView.frame = topContentRect
        topContentView.backgroundColor = MAIN_COLOR
        addSubview(topContentView)
        // add two buttons
        
        let cancelButton:UIButton = UIButton(type: .custom)
        let buttonW:CGFloat = 25.0
        let buttonX:CGFloat = 15.0
        let buttonY:CGFloat = 13.0
        cancelButton.frame = CGRect(x: buttonX, y: buttonY, width: buttonW, height: buttonW)
        cancelButton.setBackgroundImage(UIImage(named: "slect_quxiao"), for:UIControlState())
        cancelButton.addTarget(self, action: #selector(IPInputPlanSelectTimeView.inputSelectButtonAction(_:)), for: UIControlEvents.touchUpInside)
        topContentView.addSubview(cancelButton)
        
        let confirmButton:UIButton = UIButton(type: .custom)
        let confirmButtonX:CGFloat = SCREEN_W - buttonX - buttonW
        let confirmButtonY:CGFloat = buttonY
        let confirmButtonW:CGFloat = buttonW
        let confirmButtonH:CGFloat = buttonW
        confirmButton.backgroundColor = MAIN_COLOR
        confirmButton.frame = CGRect(x: confirmButtonX, y: confirmButtonY, width: confirmButtonW, height: confirmButtonH)
        confirmButton.setBackgroundImage(UIImage(named: "select_wancheng"), for: UIControlState())
        confirmButton.addTarget(self, action: #selector(IPInputPlanSelectTimeView.inputSelectButtonAction(_:)), for: UIControlEvents.touchUpInside)
        addSubview(confirmButton)
        topContentView.addSubview(confirmButton)
        
        // add datePicker
        let inputDatePickerX:CGFloat = 0
        let inputDatePickerY:CGFloat = topContentView.bottom
        let inputDatePickerW:CGFloat = SCREEN_W
        let inputDatePickerH:CGFloat = datePickerH
        let inputDatePickerFrame:CGRect = CGRect(x: inputDatePickerX,y: inputDatePickerY, width: inputDatePickerW, height: inputDatePickerH)
        inputDatePicker = UIDatePicker(frame:inputDatePickerFrame)
        inputDatePicker?.backgroundColor = UIColor.white
        addSubview(inputDatePicker!)
    
    }

      init(frame:CGRect,inputSelectTimeClosure:@escaping (String)->Void){
      super.init(frame:frame)
      self.inputPlanCloure = inputSelectTimeClosure
      setup()
       
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }

    func inputSelectButtonAction(_ sender: UIButton) {
        
        if (self.inputPlanCloure != nil){
            
            let dateFormat = DateFormatter()
            dateFormat.dateFormat = "yy年MM月dd日HH:mm:ss"
            
            let timeString = dateFormat.string(from: (inputDatePicker?.date)!)
            self.inputPlanCloure!(timeString)
            
        }
        
    }

}
